package org.zcl.rocketmq.consumer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;
import org.zcl.rocketmq.entity.Order;

/**
 * @Package org.zcl.consumer
 * @PROJECT_NAME: question
 * @DESCRIPTION:
 * @USER: zhuchenglin
 * @DATE: 2021/12/13 11:22
 */
@Slf4j
@Service
@RocketMQMessageListener(topic = "object-topic", consumerGroup = "my-consumer_object-topic")
public class TestConsumer2 implements RocketMQListener<Order> {

    @Override
    public void onMessage(Order message) {
        log.info("TestConsumer2端接收到消息:{}", message.toString());
        try {
            // 模拟处理逻辑
            boolean success = processMessage(message);

            if (!success) {
                // 如果处理失败，抛出 RuntimeException 来触发重试
                throw new RuntimeException("Message processing failed, will retry");
            }

            // 如果处理成功，不做任何操作（自动ACK）
            System.out.println("Message processed successfully");
        } catch (Exception e) {
            // 在这里捕获异常并打印日志
            e.printStackTrace();
            // 抛出异常以触发重试
            throw e;
        }
    }

    private boolean processMessage(Order message) {
        // 这里是你的消息处理逻辑
        // 返回 true 表示处理成功，false 表示处理失败
        return true;
    }
}
